<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Jobs\QiniuUploadFile;
use App\Models\Profile;
use App\Models\Qiniutask;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Request;

class ProfileController extends Controller {

	public function __construct() {
		$this->middleware('admin');
	}

	/**
	 * 进入个人资料页面
	 */
	public function getIndex() {
		$user_id = session('token')['user_id'];
		$user = User::find($user_id);
		$user->avatar = get_photo($user_id);
		$user->cardid = $user->profile->cardid;
		$data = $user->toArray();
		return view('admin/profile/profile', $data);
	}

	/**
	 * 修改个人资料
	 * @return [type] [description]
	 */
	public function postSetProfile() {
		$id = session('token')['user_id'];
		$user = User::find($id);
		if (empty($user)) {
			return $this->error('无效的用户');
		}

		if (Request::exists('password')) {
			$password = strval(Request::input('password'));
			if (!empty($password)) {
				$user->password = bcrypt($password);
			}
		}

		if (Request::exists('email')) {
			$email = strval(Request::input('email'));
			if (empty($email) OR $user->email == $email) {
				$user->email = $email;
			} else {
				if (User::where('email', $email)->count()) {
					return $this->error('邮箱已经存在');
				}
				$user->email = $email;
			}
		}

		if (Request::exists('mobile')) {
			$mobile = strval(Request::input('mobile'));
			if (empty($mobile) OR $user->mobile == $mobile) {
				$user->mobile = $mobile;
			} else {
				if (User::where('mobile', $mobile)->count()) {
					return $this->error('手机号已经被使用');
				}
				$user->mobile = $mobile;
			}
		}

		if (Request::exists('realname')) {
			$user->realname = safe(Request::input('realname'), 50);
		}
		if (Request::exists('sex')) {
			$user->sex = safe(Request::input('sex'), 10);
		}
		if (Request::exists('cardid')) {
			$cardid = safe(Request::input('cardid'), 30);
			$user->profile->cardid = $cardid;
			$user->profile->save();
		}
		$user->save();
		return $this->response($user);
	}

	public function getChangepwd() {
		return view('admin/profile/changepwd');
	}

	/**
	 * 修改密码
	 */
	public function postChangepwd() {
		$id = session('token')['user_id'];
		$user = User::find($id);
		if (empty($user)) {
			return $this->error('无效的用户');
		}

		$curpass = strval(Request::input('curpass'));
		$newpass = strval(Request::input('newpass'));

		if (!Hash::check($curpass, $user->password)) {
			return $this->error('原密码输入错误');
		}
		$user->password = Hash::make($newpass);
		$user->save();
		return $this->response();
	}

	/**
	 * 上传用户头像
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function postUploadAvatar($id = null) {
		$id = is_null($id) ? session('token')['user_id'] : intval($id);
		$avatar_data = file_get_contents('php://input');
		$data = explode('--------------------', $avatar_data);
		$targetPath = upload_path() . '/avatar';

		if (!is_dir($targetPath)) {
			if (!mkdir($targetPath, 0777, 1)) {
				$result = ['status' => '0', 'info' => '无法建立上传目录'];
				return $this->response($result);
			}
		}

		$targetFile = $targetPath . '/' . $id . '.jpg';
		if (!file_put_contents($targetFile, $data[0])) {
			return $this->response(['status' => '0', 'info' => '保存文件失败']);
		}

		$user = User::find($id);
		$user->photo = 'upload/avatar/' . $id . '.jpg';
		$user->save();
		$this->toQiniutask($user);

		return $this->response(['status' => '1', 'url' => get_photo($id)]);
	}

	/**
	 * 加入七牛上传任务队列
	 */
	private function toQiniutask(User $user) {
		$file = realpath($user->photo);
		if (file_exists($file)) {
			$task = new Qiniutask;
			$task->table_name = 'users';
			$task->field_name = 'photo';
			$task->master_id = $user->id;
			$task->file_path = $file;
			$task->target_path = '';
			$task->need_upload = 1;
			$task->need_convert = 0;
			$task->status = 0; //-1:任务处理失败,0:待处理任务,1:任务处理中,2:任务处理结束,3:任务完成
			$task->save();

			$this->dispatch(new QiniuUploadFile($task));
		}
		return true;
	}

}